package com.leet.code.simple_array;

/**
 * @description:
 * @author: WYG
 * @time: 2020/12/26 20:42
 */

public class CheckStraightLine {
    public boolean checkStraightLine(int[][] coordinates) {
        //由于除法有 / by zero的异常,故想办法把除法转变成乘法
        // (y1 - y0) / (x1 - x0) = (y2 - y1) / (x2 - x1)
        // 将上述式子转换成乘法如下:
        // (y1 - y0) * (x2 - x1) = (y2 - y1) * (x1 - x0)
        int row = coordinates.length - 1;
        for (int i = 1; i < row; i++) {
            if ((coordinates[i][1] - coordinates[i - 1][1]) * (coordinates[i + 1][0] - coordinates[i][0]) != (coordinates[i + 1][1] - coordinates[i][1]) * (coordinates[i][0] - coordinates[i - 1][0])) {
                return false;
            }
        }
        return true;
    }
}
